package createmodel

import "fmt"

/**
 * @File: builder.go
 * @Author: SunShineBoy
 * @Author: SunShineBoy@163.com
 * @Date: 2024/12/5 10:40
 * @Project: GoProject
 * @IDE: GoLand
 * @Desc: 建造者模式
**/

// Car 车
type Car interface {
	Drive()
}

// ConcreteCar 具体的车
type ConcreteCar struct {
	Wheel string // 轮子
	Chair string // 椅子
}

// Drive 隐式实现Car接口中Drive用法
func (c *ConcreteCar) Drive() {
	fmt.Printf("Car: partA=%s partB=%s\n ", c.Wheel, c.Chair)
}

// Builder 建造者接口
type Builder interface {
	BuildWheel(part string)
	BuildChair(part string)
	Build()
}

// Director 主导者
type Director struct {
	builder Builder
}

// Construct 创造产品
func (d *Director) Construct() {
	d.builder.BuildWheel("Wheel")
	d.builder.BuildChair("Chair")
	d.builder.Build()
}

// ConcreteBuilder 具体的建造者
type ConcreteBuilder struct {
	car *ConcreteCar
}

// BuildWheel 实现创造轮子方法
func (c *ConcreteBuilder) BuildWheel(part string) {
	c.car.Wheel = part
}

// BuildChair 实现创造椅子方法
func (c *ConcreteBuilder) BuildChair(part string) {
	c.car.Chair = part
}

// Build 建造完成
func (c *ConcreteBuilder) Build() {
	fmt.Println("Build done!")
}
